package it.polimi.digitalstate.webbeans;

import it.polimi.digitalstate.manager.AuthenticationManagerLocal;
import it.polimi.digitalstate.manager.ElectionManagerLocal;
import it.polimi.digitalstate.manager.MembershipManagerLocal;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.ejb.EJB;


@ManagedBean(name="canElectBean")
@RequestScoped
public class CanElectBean {
	@EJB
	private AuthenticationManagerLocal ejb_user;
	@EJB
	private ElectionManagerLocal ejb_elemanager;


	public String manageElect()
	{
//		boolean error = false;
		String Error_msg = "";
		if(!ejb_user.isLogged())
		{
			Error_msg = "You need to log into the system.";
			System.out.println(Error_msg);
			return "/login.xhtml?faces-redirect=true";
		}
		else if ( (ejb_user.getLoggedCitizen() == null)? false : ejb_elemanager.hasAlreadyElectionVoted(ejb_user.getLoggedCitizen()))
		{
			Error_msg = "You have already voted in this election.";
			System.out.println(Error_msg);
			return "/election/citizen/electionvoteerror.xhtml?faces-redirect=true";
		}
		else if (!ejb_elemanager.electionIsOpen())
		{
			Error_msg = "Election period did not start yet.";
			System.out.println(Error_msg);
			return "/election/citizen/electionvoteerror.xhtml?faces-redirect=true";
		}
		else
			return "/election/citizen/electionparty.xhtml?faces-redirect=true";
	}
	
}
